package cn.com.inossem.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.util.Base64;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;

import cn.com.inossem.systembartint.SystemBarTintManager;
import cn.com.inossem.utils.Utils;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class CareerShareDialogActivity extends Activity {
	TextView back;
	String name, url;
	String shareImage;  

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(true);
		}

		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setTintColor(getResources().getColor(R.color.app_main));

		setContentView(R.layout.shareactivity_dialog);

		name = getIntent().getStringExtra("name");
		url = getIntent().getStringExtra("url");
		initImage();

		ImageView shareweixin = (ImageView) this.findViewById(R.id.shareweixin);
		shareweixin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// finish();
				showShare();
			}
		});
		ImageView sharemail = (ImageView) this.findViewById(R.id.sharemail);
		sharemail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

//				String url_share = "http://123.57.137.89";

				String[] email = { "" }; // 需要注意，email必须以数组形式传入
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/plain"); // 设置邮件格式
				intent.putExtra(Intent.EXTRA_EMAIL, email); // 接收人
				// intent.putExtra(Intent.EXTRA_CC, email); // 抄送人
				intent.putExtra(Intent.EXTRA_SUBJECT, name); // 主题
				intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(url)); // 正文
				startActivity(Intent.createChooser(intent, "请选择邮件类应用"));
			}
		});

		back = (TextView) this.findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	
	private void initImage() {  
        try {// 判断SD卡中是否存在此文件夹  
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())  
                    && Environment.getExternalStorageDirectory().exists()) {  
                File baseFile = new File(Environment.getExternalStorageDirectory(), "share");  
                if (!baseFile.exists()) {  
                    baseFile.mkdir();  
                }  
                shareImage = baseFile.getAbsolutePath() + "/" + Utils.messageInfo.userInfo.getId() + "sharecareer.png";  
            } else {  
            	shareImage = getApplication().getFilesDir().getAbsolutePath() + "/" + Utils.messageInfo.userInfo.getId()  + "sharecareer.png";  
            }  
            File file = new File(shareImage);
            if (file.exists()) {
            	file.delete();
            }
            // 判断图片是否存此文件夹中  
            if (!file.exists()) {  
                file.createNewFile();
                String imageData = Utils.messageInfo.userInfo.getImageData();
                Bitmap pic = null;
                if (!imageData.equals("")) {
                	pic = stringtoBitmap(imageData);
                } else {
                	pic = BitmapFactory.decodeResource(getResources(), R.drawable.zhi);
                }
                FileOutputStream fos = new FileOutputStream(file);  
                pic.compress(CompressFormat.PNG, 100, fos);  
                fos.flush();  
                fos.close();  
            }  
        } catch (Throwable t) {  
            t.printStackTrace();  
            shareImage = null;  
        }  
    }  
	
	private Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	private void showShare() {
		ShareSDK.initSDK(this);
		OnekeyShare oks = new OnekeyShare();
		// 关闭sso授权
		oks.disableSSOWhenAuthorize();
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		oks.setTitle(name);
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		oks.setTitleUrl(url);
		oks.setImagePath(shareImage);
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl(url);
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite(getString(R.string.app_name));

		// 启动分享GUI
		oks.show(this);
	}

	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}
}
